package com.diors.commons.script;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * script管理类
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-16
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class ScriptManager 
{

	private final static ScriptManager instance = new ScriptManager();
	
	public static ScriptManager getInstance()
	{
		return instance;
	}
	
	private ScriptManager()
	{
		scriptSrcPath = "script";
		//scriptSrcPath = "E:/project/darren/Diors-Commons/script";
		scriptEngline = new ScriptEngline();
		scriptClasses = new HashMap<String, Class<?>>();
		scripts = new HashMap<Integer, IScript>();		
	}
	
	private String scriptSrcPath;					// 脚本源代码路径
	private ScriptEngline scriptEngline;			// 脚本引擎实例
	private Map<String, Class<?>> scriptClasses;	// 脚本Class对象
	private Map<Integer, IScript> scripts;			// 脚本实例列表
	
	/**
	 * 重新加载全部脚本 
	 * @create	2013-10-16	darren.ouyang
	 */
	public void onloads ()
	{
		List<Class<?>> classList = scriptEngline.compilePath(scriptSrcPath);
		for (Class<?> clz : classList)
			initClass(clz);
	}
	
	/**
	 * 加载class对象
	 * @param clz
	 * @create	2013-10-16	darren.ouyang
	 */
	private void initClass (Class<?> clz)
	{
		scriptClasses.put(clz.getName(), clz);
		if (IScript.class.isAssignableFrom(clz))
		{
			try {
				IScript script = (IScript) clz.newInstance();
				scripts.put(script.getID(), script);
			} 
			catch (InstantiationException e) {
				e.printStackTrace();
			} 
			catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取脚本
	 * @param id	脚本ID
	 * @return
	 * @create	2013-10-16	darren.ouyang
	 */
	public IScript getScript (int id)
	{
		return scripts.get(id);
	}
	
	/**
	 * 获取脚本对象
	 * @param id		脚本ID
	 * @param classType	脚本类型
	 * @return
	 * @create	2013-10-16	darren.ouyang
	 */
	public <T> T getScript (int id, Class<T> classType)
	{
		IScript script = getScript(id);
		if (script == null)
			return null;
		
		/* 判断classType是否是script的父类或接口 */
		if (!classType.isAssignableFrom(script.getClass()))
			return null;
		
		@SuppressWarnings("unchecked")
		T scriptT = (T)script;
		return scriptT;
	}
	
	/*public static void main(String[] args) throws InterruptedException 
	{
		PropertyConfigurator.configure("E:\\project\\darren\\Diors-Lib\\log\\log4j.properties");
		
		while (true)
		{
			ScriptManager.getInstance().onloads();
	
			for (Entry<String, Class<?>> entry : ScriptManager.getInstance().scriptClasses.entrySet())
				System.out.println(entry.getKey() + " --- " + entry.getValue());
			
			System.out.println("---------");
					
			for (Entry<Integer, IScript> entry : ScriptManager.getInstance().scripts.entrySet())
				System.out.println(entry.getKey() + " --- " + entry.getValue());
	
			System.out.println(ScriptManager.getInstance().getScript(1002, ITestAbcd.class).say());
			System.out.println(ScriptManager.getInstance().getScript(1002, ZTestAbcd.class).sayd());
			
			System.out.println("----------------- 一次结束 -----------------");
			
			Thread.sleep(3000);
		}
		//scriptSrcPath = "E:\\project\\darren\\Diors-Commons\\script";
		//scriptSrcPath = "E:\\project\\darren\\Diors-Commons\\script\\";
		//scriptSrcPath = "script\\";
		//scriptSrcPath = "script";
		//scriptSrcPath = "\\script";
		//scriptSrcPath = "script/";
		//scriptSrcPath = "E:/project/darren/Diors-Commons/script";
		//scriptSrcPath = "E:/project/darren/Diors-Commons/script/";
		//scriptSrcPath = "E:/project\\darren/Diors-Commons\\script/";
	}*/
	
}
